package com.example.springboot4.system.config;

import cn.hutool.core.util.RandomUtil;
import io.swagger.v3.oas.annotations.ExternalDocumentation;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.enums.SecuritySchemeIn;
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType;
import io.swagger.v3.oas.annotations.info.Contact;
import io.swagger.v3.oas.annotations.info.Info;
import io.swagger.v3.oas.annotations.info.License;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.security.SecurityScheme;
import io.swagger.v3.oas.annotations.servers.Server;
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * Swagger配置
 *
 * @author 宋义州
 * @since 2024/5/21 下午5:34
 */
@Configuration
//Api信息配置
@OpenAPIDefinition(
        // API的基本信息，包括标题、版本号、描述、联系人等
        info = @Info(
                title = "Swagger3.0 (Open API) 框架学习示例文档",      // Api接口文档标题（必填）
                description = "学习Swagger框架而用来定义测试的文档",     // Api接口文档描述
                version = "1.0",                                    // Api接口版本
                termsOfService = "https://example.com/",            // Api接口的服务条款地址
                contact = @Contact(
                        name = "宋义州",                             // 作者名称
                        email = "s_yizhou@163.com",                 // 作者邮箱
                        url = "https://www.cnblogs.com/antLaddie/"  // 介绍作者的URL地址
                ),
                license = @License(                                                // 设置联系人信息
                        name = "Apache 2.0",                                       // 授权名称
                        url = "https://www.apache.org/licenses/LICENSE-2.0.html"   // 授权信息
                )
        ),
        // ## 表示服务器地址或者URL模板列表，多个服务地址随时切换（只不过是有多台IP有当前的服务API）
        servers = {
                @Server(url = "http://localhost:10086/example-serve/", description = "本地服务器一服务"),
                @Server(url = "http://localhost:10086/example-serve/", description = "本地服务器二服务"),
        },
        externalDocs = @ExternalDocumentation(description = "更多内容请查看该链接", url = "xxx"),
        security = @SecurityRequirement(name = "Authorization"))
// 认证方案配置
@SecurityScheme(
        name = "Authorization",              // 认证方案名称
        type = SecuritySchemeType.HTTP,      // 认证类型，当前为http认证
        description = "这是一个认证的描述详细",  // 描述信息
        in = SecuritySchemeIn.HEADER,        // 代表在http请求头部
        scheme = "Bearer",                   // 认证方案，如：Authorization: bearer token信息
        bearerFormat = "JWT",
        paramName = "Authorization")
public class SwaggerConfig {

    /**
     * 根据@Tag 上的排序，写入x-order
     *
     * @return the global open api customizer
     */
    @Bean
    public GlobalOpenApiCustomizer orderGlobalOpenApiCustomizer() {
        return openApi -> {
            if (openApi.getTags() != null) {
                openApi.getTags().forEach(tag -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("x-order", RandomUtil.randomInt(0, 100));
                    tag.setExtensions(map);
                });
            }
            if (openApi.getPaths() != null) {
                openApi.addExtension("x-test123", "333");
                openApi.getPaths().addExtension("x-abb", RandomUtil.randomInt(1, 100));
            }

        };
    }

}
